import request from '@/modules/index/utils/request';
import { downloadFile } from '@/utils/common';
import { Message } from 'element-ui';
import qs from 'qs';
const store = {
  actions: {
    // 我的预测-查询
    async proAdminPredicttaskPredictTaskSearch (context, params) {
      const data = await request({
        url: '/predicttask/predict-task/search',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 我的预测-下载
    async proAdminPredicttaskPredictTaskIdGetResultUrl (context, params) {
      const data = await request({
        url: `/predicttask/predict-task/${params.id}/getResultUrl`,
        method: 'get',
        responseType: 'blob'
      });
      if (!data.data) {
        Message.error("生成文件失败");
        return;
      }
      downloadFile(data);
    },
    // 我的项目-无监督学习-下载
    async proAdminProjectIdGetResultFile (context, params) {
      const data = await request({
        url: `/project/${params.id}/getResultFile`,
        method: 'get',
        responseType: 'blob'
      });
      if (!data.data) {
        Message.error("生成文件失败");
        return;
      }
      downloadFile(data);
    },
    // jupyterhub
    async jupyterhubFind (context, params) {
      const res = await request({
        url: '/jupyterhub/find',
        method: 'get',
        params: params
      });
      const result = res.data || {};
      if (result.success) {
        const data = result.data || {};
        const juParams = {
          username: data.userName || '',
          password: data.userPasswd || ''
        };
        // 获取jupyter跳转地址
        const jRes = await request({
          url: '/jupyterhub/findUrl',
          method: 'get',
        });
        const jResult = jRes.data || {};
        if (jResult.success) {
          let url = jResult.data || '';
          if (url) {
            url = `http://${url}/hub/login?next=`;
          }
          // const url = 'http://10.17.157.85:8090/hub/login?next=';
          // const url = 'http://192.126.14.2:8090/hub/login?next='
          const temp = document.createElement('form');
          temp.action = url;
          temp.method = 'post';
          temp.style.display = 'none';

          for (let x in juParams) {
            const opt = document.createElement('textarea');
            opt.name = x;
            opt.value = juParams[x];
            temp.appendChild(opt);
          }

          document.body.appendChild(temp);
          temp.submit();
        } else {
          Message.error("获取jupyter地址失败");
        }
      } else {
        Message.error("获取jupyter用户信息失败");
      }
    },
    // 获取模型导入列表
    async proAdminImportModelTaskSearch (context, params) {
      const data = await request({
        url: '/import-model-task/search',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 导入模型
    async proAdminImportModelTaskSave (context, params) {
      const data = await request({
        url: '/import-model-task/save',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 删除模型
    async proAdminImportModelTaskBatchDelete (context, params) {
      const data = await request({
        url: '/import-model-task/batchDelete',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 模型导入的模型上传
    async proAdminImportModelUploadId (context, params) {
      const data = await request({
        url: `/import-model-task/upload/${params.id}`,
        method: 'post',
        data: params.file,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
      return data;
    },
    // 发布外部模型
    async proAdminImportModelTaskPublishExternalModel (context, params) {
      const data = await request({
        url: '/import-model-task/publishExternalModel',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 下线外部模型
    async proAdminImportModelTaskOfflineExternalModel (context, params) {
      const data = await request({
        url: '/import-model-task/offlineExternalModel',
        method: 'post',
        data: params,
      });
      return data;
    },
    // 模型-预测
    async proAdminImportModelTaskIdImportModelPredict (context, params) {
      const data = await request({
        url: `/import-model-task/${params.id}/import-model-predict`,
        method: 'post',
        data: params.file,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      });
      return data;
    },
  }
};

export default store;
